package com.testmall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.testmall.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: littleDesire
 * @Date: 2022-06-21 14:29
 * @Version: 1.0
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    /**
     * 注册用户
     *
     * @param user 用户对象
     * @return 插入的行数
     */
    int registerUser(User user);

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 查询的行数
     */
    int login(Map<String, Object> map);

    /**
     * 查询用户是否存在
     *
     * @param username 用户名
     * @return 用户对象
     */
    @Select("select username, `password`, salt, phone from user where username = #{username}")
    User selectByUsername(@Param("username") String username);

    /**
     * 查询传入的电话在数据库是否存在
     * @param phone 要查询的电话
     * @return 查询的记录数
     */
    @Select("select count(1) from user where phone = #{phone}")
    int selectByPhone(@Param("phone") String phone);
    @Select("select username, `password`, salt, phone from user where phone = #{phone}")
    User selectbyPhone2 (@Param("phone")String phone);

    @Update("update user set password = #{newPassword} where username = #{username}")
    int changePassword(@Param("newPassword") String newPassword, @Param("username") String username);

    @Select("select userid,`role`,username,phone from user")
    List<Map<String, Object>> queryUser();
}
